/**
 * Created by bian on 16-3-9.
 */
//css
require('../admin-base/common.js');
require('../../css/admin/shops-ctrl.scss');
require('../../css/admin/goods.scss');
//js

var $ = jQuery;
require('angular');
require('angular-route');
require('../angular.simple-datatables.js');

var app = angular.module('app', ['simpleDatatable', 'ngRoute']);


var getGoodsData = function ($http, scope, status) {
    if (status === 0 ? !scope.data.uncheck : !scope.data.checked) {
        $http
            .get('/adminer-super/shops-ctrl/data')
            .success(function (data) {
                console.log(data);
                for(var i in data) {
                    data[i].mainImg = '<img src="' + data[i].mainImg + '" >';
                }
                scope.data = data;
                scope.list = scope.data;
                scope.$applyAsync();
            })
    } else {
        scope.list = status === 0 ? scope.data.inactive : scope.data.active;
        scope.$applyAsync();
    }
};

app.controller('AppCtrl', ['$scope', '$http', '$sce', function (scope, $http, $sce) {

    scope.data = {
    };

    scope.list = [];

    getGoodsData($http, scope, 1);

    //1上2下，-1删除
    scope.sdtOn = function (event, row) {
        console.log(row.id);
        var extraParams = Array.prototype.slice.call(arguments, 2);
        var status = 0;
        var remover = extraParams[1];

        if (extraParams[0] === 'up') {
            status = 1;
            row.status = status;
            if (scope.data) {
                scope.data.push(row);
            }
        } else if (extraParams[0] === 'down') {
            status = 0;
            row.status = status;
            if (scope.data) {
                scope.data.push(row);
            }
        } else if (extraParams[0] === 'del') {
            status = -1;
        }

        $http
            .post('/adminer-super/shops-ctrl/del', {
                id: row.id
            })
            .success(function (ret) {
                if (ret.status) {
                    remover();
                }
            })
            .error(function () {
                alert('操作失败，请刷新重试');
            });
    };

    scope.actionColFactory =  angular.element('#row-btn').html();

    window.s = scope;
}]);


angular.bootstrap(document.documentElement, ['app']);